package com.ghca.myinfo;

import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
import org.springframework.data.web.config.EnableSpringDataWebSupport;
import org.springframework.scheduling.annotation.EnableScheduling;
import redis.clients.jedis.JedisPool;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.handler.CompositePageProcessor;
import us.codecraft.webmagic.handler.SubPageProcessor;
import us.codecraft.webmagic.pipeline.Pipeline;
import us.codecraft.webmagic.scheduler.RedisScheduler;
import us.codecraft.webmagic.scheduler.Scheduler;

import java.util.List;

@SpringBootApplication
@EnableSpringDataWebSupport
@EnableScheduling
public class MyinfoAllApplication {

	public static void main(String[] args) {

		ConfigurableApplicationContext context = SpringApplication.run(MyinfoAllApplication.class, args);
		/*User test = (User) context.getBean(UserService.class).loadUserByUsername("test");
		context.getBean(PermissionService.class).findRootPermission(test.getRoles());
		context.getBean(PermissionService.class).findByParent(test.getRoles(), "1");
		System.out.println(test);*/

		/*OOSpider.create(Site.me().setTimeOut(12000).setSleepTime(1000), TenderNotice.class).
				setExitWhenComplete(true).setScheduler(new RedisPriorityScheduler("27.126.180.192")).addUrl("https://www.sczbbx.com/GCJS/bxgg.aspx").start();
*/

		/*Spider.create(context.getBean(CompositePageProcessor.class)).addPipeline(context.getBean(Pipeline.class))
				.setScheduler(context.getBean(Scheduler.class))
				.addUrl("https://www.sczbbx.com/GCJS/bxgg.aspx").start();*/
	}

	@Bean
	public CompositePageProcessor compositePageProcessor(List<SubPageProcessor> subPageProcessors) {
		CompositePageProcessor pageProcessor = new CompositePageProcessor(Site.me().setSleepTime(0));
		for(SubPageProcessor subPageProcessor : subPageProcessors) {
			pageProcessor.addSubPageProcessor(subPageProcessor);
		}
		return pageProcessor;
	}

	@Bean
	public PageableHandlerMethodArgumentResolver pageableResolver() {
		PageableHandlerMethodArgumentResolver  pageableResolver = new PageableHandlerMethodArgumentResolver();
		pageableResolver.setPageParameterName("page");
		pageableResolver.setSizeParameterName("rows");
		return pageableResolver;
	}

	@Bean
	public Scheduler scheduler() {
		return  new RedisScheduler("127.0.0.1") {
			@Override
			protected boolean shouldReserved(Request request) {
				if(request.getUrl().equals("https://www.sczbbx.com/GCJS/bxgg.aspx")) {
					return true;
				}
				return super.shouldReserved(request);
			}
		};
	}


	/*@Bean
	@ConditionalOnClass(FastJsonHttpMessageConverter.class)
	public FastJsonHttpMessageConverter fastJsonHttpMessageConverters() {
		FastJsonHttpMessageConverter fastJsonHttpMessageConverters = new FastJsonHttpMessageConverter();
		return fastJsonHttpMessageConverters;
	}*/



}
